package com.yidu.BusinessData.mapper;

import com.yidu.BusinessData.entity.assetValuation;
import com.yidu.BusinessManagement.entity.DealData;
import com.yidu.InventoryManagement.entity.CashAccrualStock;
import com.yidu.InventoryManagement.entity.SecurityAccrualStock;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author z
 * @version 1.0
 * @date 2022/12/12
 */
@Mapper
public interface assetValuationMapper {
    /**
     * 查询
     * @param map
     * @return
     */
    @Select("SELECT s.`unitCost`,s.`quantity`,ss.`securityName`,ss.`securityId`,m.`closingPrice`,a.* FROM securityStock s INNER JOIN marketData m ON s.`securityId`=m.`securityId` INNER JOIN SECURITY ss ON s.`securityId`=ss.`securityId` INNER JOIN assetValuation a ON ss.`securityName`=a.`assetName`")
    List<assetValuation> selectAssetValuation(Map map);


    //算资产估值
    @Select ("SELECT *,s.*,(quantity*closingPrice)-(quantity*unitCost) number FROM \n" +
            "(SELECT * FROM securityStock \n" +
            "WHERE DATE=#{assetDate}) s \n" +
            "LEFT JOIN \n" +
            "(SELECT * FROM marketData WHERE TIME=#{assetDate}) m \n" +
            "ON s.`securityId`=m.`securityId` \n" +
            "INNER JOIN SECURITY ss \n" +
            "ON s.`securityId`=ss.`securityId` \n" +
            "INNER JOIN assetValuation a \n" +
            "ON ss.`securityName`=a.`assetName`")
    List<assetValuation> selectAssetValuations(Map map);
    //清算款
  @Select ( "SELECT securityId,dealTime,dealState,SUM(endMoney * dealWay ) endMoney,fundId FROM DealData " +
          "            WHERE dealTime <= #{dealTime} AND endTime> #{dealTime}  AND dealWay IN(1,2,3,4)" +
          "           GROUP BY securityId,dealTime,dealState")
            List<DealData> selectAssetValuationss(String dealTime);

    //删除证券应收应付库存的方法
    @Delete("delete from securityAccrualStock where securityId=#{securityAccrualStockId} and fundId=#{fundId} and DATE=#{date} and businessState=#{businessState}")
    public int deleteSecuritiesInventoryInfos(SecurityAccrualStock securityAccrualStock);

    //查交易数据 按证券代码分组 插入证券应收应付库存
    public void selectDealData(HashMap hashMap);
    //查交易数据后 按条件删除 证券应收应付表的内容

    //删除证券应收应付库存的方法
    @Delete("delete from securityAccrualStock where securityId=#{securityAccrualStockId} and fundId=#{fundId} and DATE=#{date} and businessState=#{businessState}")
    public int deleteSecuritiesInventoryInfoTwo(SecurityAccrualStock securityAccrualStock);

    //查询ta交易数据 按账户分组
    public void selectTaTransaction(HashMap hashMap);

    //按条件删除现金应收应付库存
    public int deleteCashAccrualStock(CashAccrualStock cashAccrualStock);

    @Select("select money from ta where dealTime=#{dealTime}")
    public double selectMoneyByDate(String dealTime);
}
